<template>
  <el-container>
    <el-header>
      <!-- 头部 -->
      <div class="header_logo">
        <img src="../assets/logo.png" alt="" />
        <span>小狗狗管理系统</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <!-- 主区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 一级菜单 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          :default-active="activePath"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="menu.id.toString()"
            v-for="menu in menusList"
            :key="menu.id"
          >
            <template slot="title">
              <i class="el-icon-toilet-paper"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单  -->
            <el-menu-item
              :index="'/' + childrenMenu.path"
              v-for="childrenMenu in menu.children"
              :key="childrenMenu.id"
              @click="saveactivePath('/' + childrenMenu.path)"
            >
              <i class="el-icon-s-data"></i>
              {{ childrenMenu.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <!-- 欢迎界面 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      //菜单列表
      menusList: [],
      isCollapse: false,
      // 用来保存链接地址
      activePath: "",
    };
  },
  methods: {
    logout() {
      //清除token
      sessionStorage.clear(); //清除所有数据
      // 跳转到登录页面
      this.$router.push("/login");
    },
    //点击按钮,切换菜单栏的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存被激活的链接地址
    saveactivePath(activedPath) {
      window.sessionStorage.setItem("activedPath", activedPath);
      // 激活被选
      this.activePath = sessionStorage.getItem("activedPath");
    },
  },

  //Home组件需要在创建后请求渲染,Vue生命周期函数
  created() {
    this.$http.get("/menus").then(
      (response) => {
        this.menusList = response.data.data;
      },
      (error) => {
        console.log("访问请求失败", error);
      }
    );
    // 激活导航栏被选中样式
    this.activePath = window.sessionStorage.getItem("activedPath");
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.toggle-button {
  background-color: rgb(109, 130, 151);
  font-size: 18px;
  line-height: 36px;
  text-align: center;
  letter-spacing: 0.2em;
  color: #fff;
  // 鼠标样式
  cursor: pointer;
}
.el-header {
  background-color: rgb(230, 222, 220);
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  > button {
    border-radius: 20px;
  }
  .header_logo {
    height: 100%;
    display: flex;
    align-items: center;
    img {
      height: 100%;
      border-radius: 50%;
    }
    span {
      color: rgb(184, 81, 81);
    }
  }
}
.el-aside {
  background-color: rgb(84, 92, 100);
  .el-menu {
    border-right: 0px solid;
  }
}
.el-main {
  background-color: rgb(245, 245, 245);
}
</style>